#include "listStruct.h"
#include <stdlib.h>
#include <stdio.h>

//1.新建一个队列.（首尾为自身）,长度为1，返回Listp
Listp create_list(void)
{
    //1-1申请List的空间
    List * L;
    if((L = (List *)malloc(sizeof(List))) == NULL)
    {
        printf("malloc linkqueue failed.\n");
        return NULL;
    }

    //1-2申请ListNode中的空间，完成初始化
    L->front = L->rear = (ListNode *)malloc(sizeof(ListNode));
    if(L->front == NULL)
    {
        printf("malloc node failed.\n");
        return L;
    }

    L->front->data.x = 0;
    L->front->data.y = 0;
    L->front->pNext = NULL;
    L->length++;
    return L;
}



//2.新建单个节点,需要参数：队列Listp, 节点ElemType_t数据；返回Nodep
ListNodep create_node(uint16_t x, uint16_t y)
{
    ListNodep Np;
    if((Np=(ListNode *)malloc(sizeof(ListNode)))== NULL)
    {
        printf("malloc node failed.\n");
        return Np;
    }
    Np->data.x = x;
    Np->data.y = y;
    Np->pNext = NULL;
    Np->pPrev = NULL;
    
    return Np;
}

//3.尾插队列
int list_tail_insert(Listp listp, ListNodep nodep)
{
    if(listp==NULL)
    {
        printf("The list is invalid.\n");
        return -1;
    }

    //1. 对nodep的设置
    nodep->pNext = NULL;//非循环链表
    nodep->pPrev = listp->rear;

    //2. 对listp的设置
    listp->rear->pNext = nodep;
    listp->rear = nodep;
    listp->length++;

    return 0;
}

//4.头插、头出、删除队列元素（暂时没需求)

//5.遍历整个list
void traverse_list(Listp listp, int mode)
{
    if(listp==NULL)
    {
        printf("The list is invalid.\n");
        return;
    }

    ListNodep curNode = NULL;
    if(mode == 1)//倒序打印
    {
        curNode = listp->rear;
    }
    else if(mode == 0)//顺序打印
    {
        curNode = listp->front;
    }

    int i = listp->length;
    while(i--)
    {
        //打印出相应点的x, y坐标值
        printf("x= %d, y=%d\n", curNode->data.x, curNode->data.y);
        if(mode == 1)
        {
            curNode = curNode->pPrev;
        }
        else
        {
            curNode = curNode->pNext;
        }
    
    }
}
//6.查找队列元素



//6. 销毁整个队列
int destroy_list(void)
{

}


void list_module_test(void)
{
    Listp lp = create_list();//(0, 0)
    ListNodep nodep = create_node(10, 20);
    list_tail_insert(lp, nodep);

    nodep = create_node(12, 40);
    list_tail_insert(lp, nodep);

    nodep = create_node(34, 53);
    list_tail_insert(lp, nodep);

    nodep = create_node(77, 33);
    list_tail_insert(lp, nodep);

    traverse_list(lp, 1);
}